package com.hopes.bizz.poll.alarm.message;

import cn.hutool.core.collection.CollUtil;
import com.hopes.boss.common.core.util.SpringContextHolder;
import com.hopes.boss.common.websocket.distribute.MessageDO;
import com.hopes.boss.common.websocket.distribute.RedisMessageDistributor;
import lombok.experimental.UtilityClass;

import java.util.List;

/**
 * @author daijie
 * @version 1.0
 * @date 2023/10/17 10:05
 * @Description:
 */
@UtilityClass
public class AlarmMessageDistributor {

    /**
     * 指定目标用户消息
     *
     * @param needBroadcast 是否广播
     * @param remindUserList 目标用户ID
     * @param messageText 消息内容
     */
    public void distribute(Boolean needBroadcast, List<Object> remindUserList, String messageText) {
        RedisMessageDistributor messageDistributor = SpringContextHolder.getBean(RedisMessageDistributor.class);

        MessageDO messageDO = new MessageDO();
        messageDO.setNeedBroadcast(needBroadcast);
        messageDO.setSessionKeys(remindUserList);
        messageDO.setMessageText(messageText);
        messageDistributor.distribute(messageDO);
    }

    /**
     * 广播消息
     *
     * @param messageText 消息内容
     */
    public void broadcastMessage(String messageText) {
        RedisMessageDistributor messageDistributor = SpringContextHolder.getBean(RedisMessageDistributor.class);

        MessageDO messageDO = new MessageDO();
        messageDO.setNeedBroadcast(Boolean.TRUE);
        messageDO.setSessionKeys(CollUtil.newArrayList(1));
        messageDO.setMessageText(messageText);
        messageDistributor.distribute(messageDO);
    }
}
